package com.zf.tree;

import java.security.PublicKey;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        //创建二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建所需要的节点
        HeroNode root = new HeroNode(1,"宋江");
        HeroNode node01 = new HeroNode(2,"吴用");
        HeroNode node02 = new HeroNode(3,"卢俊义");
        HeroNode node03 = new HeroNode(4,"林冲");
        HeroNode node04 = new HeroNode(5,"张益达");

        //手动创建二叉树，后面用递归方式实现
        binaryTree.setRoot(root);
        root.setLeft(node01);
        root.setRight(node02);
        node02.setRight(node03);
        node02.setLeft(node04);
//        //测试
//        //1, 2, 3, 4
//        System.out.println("前序遍历");
//        binaryTree.preOrder();

//        //测试
//        //2, 1, 3, 4
//        System.out.println("中序遍历");
//        binaryTree.infixOrder();
//
//        //测试
//        //2, 4, 3, 1
//        System.out.println("后序遍历");
//        binaryTree.postOrder();
//
//        //测试查找
//        HeroNode resNode = null;
//        System.out.println("前序遍历方式");
//        resNode = binaryTree.preOrderSearch(15);
//        if (resNode != null){
//            System.out.printf("找到了，信息为no=%d, name=%s\n",resNode.getNo(), resNode.getName());
//        }else {
//            System.out.printf("没有找到id=%d的英雄\n",15);
//        }

        //测试删除节点
        System.out.println("删除前：前序遍历");
        //1, 2, 3, 5, 4
        binaryTree.preOrder();

        System.out.println("删除后：前序遍历");
        binaryTree.delNode(3);
        //1, 2
        binaryTree.preOrder();



    }

}
class BinaryTree{
    private HeroNode root;


    //遍历删除
    public void delNode(int no){
        if (root.getNo() == no){
            root = null;
        }else {
            root.delNode(no);
        }
    }

    public void setRoot(HeroNode root) {
        this.root = root;
    }
    //前序遍历
    public void preOrder(){
        if (this.root != null){
            this.root.preOrder();
        }else {
            System.out.println("当前二叉树为空，无法遍历");
        }
    }

    //中序遍历
    public void infixOrder(){
        if (this.root != null){
            this.root.infixOrder();
        }else {
            System.out.println("当前二叉树为空，无法遍历");
        }
    }
    //后序遍历
    public void postOrder(){
        if (this.root != null){
            this.root.postOrder();
        }else {
            System.out.println("当前二叉树为空，无法遍历");
        }
    }

    //前序遍历查找
    public HeroNode preOrderSearch(int no){
        HeroNode resNode = null;
        if (this.root != null){
            resNode = this.root.preOrderSearch(no);
        }
        return resNode;
    }
    //中序遍历查找
    public HeroNode infixOrderSearch(int no){
        HeroNode resNode = null;
        if (this.root != null){
            resNode = this.root.infixOrderSearch(no);
        }
        return resNode;
    }
    //后序遍历查找
    public HeroNode postOrderSearch(int no){
        HeroNode resNode = null;
        if (this.root != null){
            resNode = this.root.postOrderSearch(no);
        }
        return resNode;
    }
}
class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
    //前序遍历的方法
    public void preOrder(){
        //先输出父节点
        System.out.println(this);
        if (this.left != null){
            this.left.preOrder();
        }
        if (this.right != null){
            this.right.preOrder();
        }
    }
    //中序遍历
    public void infixOrder(){
        if (this.left != null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null){
            this.right.infixOrder();
        }
    }
    //后序遍历
    public void postOrder(){
        if (this.left != null){
            this.left.postOrder();
        }
        if (this.right != null){
            this.right.postOrder();
        }
        System.out.println(this);
    }

    //前序遍历查找
    public HeroNode preOrderSearch(int no){
        System.out.println("进入前序遍历查找~~");
        if (this.no == no){
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null){
            //说明找到
            return resNode;
        }
        if (this.right != null){
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    //中序遍历查找
    public HeroNode infixOrderSearch(int no){
        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.infixOrderSearch(no);
        }

        //如果找到，则返回，如果没有找到，就和当前节点比较，如果是则返回当前节点
        if (resNode != null){
            return resNode;
        }
        if (this.no == no){
            return this;
        }
        //否则向右子节点递归中序查找
        if (this.right != null){
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }

    //后序遍历查找
    public HeroNode postOrderSearch(int no){
        HeroNode resNode = null;
        if (this.left != null){
            resNode = this.left.postOrderSearch(no);
        }
        //找到即返回
        if (resNode != null){
            return resNode;
        }
        //否则右子节点如果不为空，遍历右子节点递归查找
        if (this.right != null){
            resNode = this.right.postOrderSearch(no);
        }
        //找到即返回
        if (resNode != null){
            return resNode;
        }
        //否则和当前节点进行比较，是则返回，否则返回null
        if (this.no == no){
            return this;
        }
        return resNode;
    }

    //递归删除
    public void delNode(int no){

        //1.如果左子节点不为空，且左子节点就是需要删除的节点，则将this.left = null;并返回
        if (this.left != null && this.left.no == no){
            this.left = null;
            return;
        }

        //2.如果右子节点不为空，且右子节点就是需要删除的节点，则将this.right = null;并返回
        if (this.right != null && this.right.no == no){
            this.right = null;
            return;
        }

        //3.如果1，2步没有删除，则向左子树进行递归删除
        if (this.left != null) {
            this.left.delNode(no);
        }

        //4.如果1，2，3步没有删除，则向右子树进行递归删除
        if (this.right != null) {
            this.right.delNode(no);
        }
    }
}
